package com.geovis.emergency.spd.biz.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.system.mapper.SystemParamMapper;
import com.geovis.emergency.spd.biz.system.service.ISystemParamService;
import com.geovis.emergency.spd.entity.system.entity.SystemParam;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemParamSaveDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * <p>
 * 系统管理模块-参数设置 服务实现类
 * </p>
 *
 * @author 王响
 * @since 2022-04-08
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class SystemParamServiceImpl extends ServiceImpl<SystemParamMapper, SystemParam> implements ISystemParamService {

    @Override
    public void save(SystemParamSaveDTO saveDTO) {
        SystemParam select = super.getById(saveDTO.getId());
        Assert.isNull(select, "该编码【{}】已经存在!", saveDTO.getId());
        SystemParam systemParam = BeanUtil.toBean(saveDTO, SystemParam.class);
        save(systemParam);
    }

    @Override
    public void update(SystemParamSaveDTO updateDTO) {
        SystemParam systemParam = super.getById(updateDTO.getId());
        Assert.notNull(systemParam, "id:{},库里没有查询要相关数据");
        BeanUtil.copyProperties(updateDTO, systemParam);
        updateById(systemParam);
    }

    private final CacheManager cacheManager;

    @Override
    public String getValueById(String id) {
        SystemParam systemParam = super.getById(id);
        Assert.notNull(systemParam,"【{}】参数没有设置,请设置!");
        Assert.notEmpty(systemParam.getValue(),"【{}】参数没有设置值,请设置!");
        return systemParam.getValue();
    }

    @Override
    public void removeByIdList(Set<String> idList) {
        if (CollUtil.isEmpty(idList)) {
            return;
        }
        //1、刪除本身
        removeByIds(idList);
    }
}
